<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-select v-model="queryParams.name" placeholder="请选择人员姓名" clearable size="small">
          <el-option
            v-for="dict in filteredUsers"
            :key="dict.userId"
            :label="dict.nickName "
            :value="dict.userId"
          />
        </el-select>
<!--        <el-input-->
<!--          v-model="queryParams.name"-->
<!--          placeholder="请输入姓名"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
      </el-form-item>
<!--      <el-form-item label="岗位" prop="post">-->
<!--        <el-input-->
<!--          v-model="queryParams.post"-->
<!--          placeholder="请输入岗位"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="证书" prop="certificate">-->
<!--        <el-input-->
<!--          v-model="queryParams.certificate"-->
<!--          placeholder="请输入证书"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="颁证机构" prop="awardingBodies">-->
<!--        <el-input-->
<!--          v-model="queryParams.awardingBodies"-->
<!--          placeholder="请输入颁证机构"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="证书编号" prop="certificateCode">-->
<!--        <el-input-->
<!--          v-model="queryParams.certificateCode"-->
<!--          placeholder="请输入证书编号"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="初领日期" prop="certificateDate">-->
<!--        <el-date-picker clearable size="small"-->
<!--          v-model="queryParams.certificateDate"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="选择初领日期">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="有效期开始时间" prop="validityPeriodBegin">-->
<!--        <el-date-picker clearable size="small"-->
<!--          v-model="queryParams.validityPeriodBegin"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="选择有效期开始时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="有效期截止时间" prop="validityPeriodEnd">-->
<!--        <el-date-picker clearable size="small"-->
<!--          v-model="queryParams.validityPeriodEnd"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="选择有效期截止时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="下次复审、换证时间" prop="renewalTime">-->
<!--        <el-date-picker clearable size="small"-->
<!--          v-model="queryParams.renewalTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="选择下次复审、换证时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="证件类型" prop="documentType">-->
<!--        <el-select v-model="queryParams.documentType" placeholder="请选择证件类型" clearable size="small">-->
<!--          <el-option-->
<!--            v-for="dict in documentTypeOptions"-->
<!--            :key="dict.dictValue"-->
<!--            :label="dict.dictLabel"-->
<!--            :value="dict.dictValue"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用" prop="remarkSpare">-->
<!--        <el-input-->
<!--          v-model="queryParams.remarkSpare"-->
<!--          placeholder="请输入备用"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用1" prop="remarkSpare1">-->
<!--        <el-input-->
<!--          v-model="queryParams.remarkSpare1"-->
<!--          placeholder="请输入备用1"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用2" prop="remarkSpare2">-->
<!--        <el-input-->
<!--          v-model="queryParams.remarkSpare2"-->
<!--          placeholder="请输入备用2"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ledger:certificates:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ledger:certificates:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ledger:certificates:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['ledger:certificates:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="certificatesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="主键ID" align="center" prop="id" />-->
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="岗位" align="center" prop="post" />
      <el-table-column label="证书" align="center" prop="certificate" />
      <el-table-column label="颁证机构" align="center" prop="awardingBodies" />
      <el-table-column label="证书编号" align="center" prop="certificateCode" />
      <el-table-column label="初领日期" align="center" prop="certificateDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.certificateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期开始时间" align="center" prop="validityPeriodBegin" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.validityPeriodBegin, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期截止时间" align="center" prop="validityPeriodEnd" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.validityPeriodEnd, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下次复审、换证时间" align="center" prop="renewalTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.renewalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="证件类型" align="center" prop="documentType" :formatter="documentTypeFormat" />
      <el-table-column label="备注" align="center" prop="remark" />
<!--      <el-table-column label="备用" align="center" prop="remarkSpare" :formatter="remarkSpareFormat" />-->
<!--      <el-table-column label="备用1" align="center" prop="remarkSpare1" :formatter="remarkSpare1Format" />-->
<!--      <el-table-column label="备用2" align="center" prop="remarkSpare2" :formatter="remarkSpare2Format" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ledger:certificates:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ledger:certificates:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改人员其他资质证件清单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
        <el-form-item label="姓名" prop="name">
          <el-select  v-model="form.name" filterable placeholder="选择人员" @change="handleChange"  style="width: 100%">
            <el-option
              v-for="user in filteredUsers"
              :key="user.userId+''"
              :label="user.nickName"
              :value="user.userId+''">
            </el-option>
          </el-select>
<!--          <el-input v-model="form.name" placeholder="请输入姓名" />-->
        </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
        <el-form-item label="岗位" prop="post">
          <el-select v-model="form.post" placeholder="请选择"  style="width: 100%">
            <el-option
              v-for="item in postOptions"
              :key="item.postId"
              :label="item.postName"
              :value="item.postId"
              :disabled="item.status == 1"
            ></el-option>
          </el-select>
<!--          <el-input v-model="form.post" placeholder="请输入岗位" />-->
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
          <el-form-item label="证件类型" prop="documentType">
            <el-select v-model="form.documentType" placeholder="请选择证件类型"  style="width: 100%">
              <el-option
                v-for="dict in documentTypeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
        <el-form-item label="证书" prop="certificate">
          <el-input v-model="form.certificate" placeholder="请输入证书" />
        </el-form-item>
          </el-col>
        </el-row>


        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
        <el-form-item label="证书编号" prop="certificateCode">
          <el-input v-model="form.certificateCode" placeholder="请输入证书编号" />
        </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
          <el-form-item label="颁证机构" prop="awardingBodies">
            <el-input v-model="form.awardingBodies" placeholder="请输入颁证机构" />
          </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
          <el-form-item label="初领日期" prop="certificateDate">
            <el-date-picker clearable size="small"
                            v-model="form.certificateDate"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择初领日期"  style="width: 100%">
            </el-date-picker>
          </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
        <el-form-item label="有效期开始时间" prop="validityPeriodBegin">
          <el-date-picker clearable size="small"
            v-model="form.validityPeriodBegin"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择有效期开始时间"  style="width: 100%">
          </el-date-picker>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
        <el-form-item label="有效期截止时间" prop="validityPeriodEnd">
          <el-date-picker clearable size="small"
                          v-model="form.validityPeriodEnd"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择有效期截止时间"  style="width: 100%">
          </el-date-picker>
        </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
        <el-form-item label="下次复审、换证时间" prop="renewalTime">
          <el-date-picker clearable size="small"
            v-model="form.renewalTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择下次复审、换证时间"  style="width: 100%">
          </el-date-picker>
        </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" type="textarea"/>
        </el-form-item>
<!--        <el-form-item label="备用" prop="remarkSpare">-->
<!--          <el-input v-model="form.remarkSpare" placeholder="请输入备用" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用1" prop="remarkSpare1">-->
<!--          <el-input v-model="form.remarkSpare1" placeholder="请输入备用1" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用2" prop="remarkSpare2">-->
<!--          <el-input v-model="form.remarkSpare2" placeholder="请输入备用2" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCertificates, getCertificates, delCertificates, addCertificates, updateCertificates, exportCertificates ,optionselect} from "@/api/ledger/certificates";
import { listUserNoPage } from '@/api/system/user'

export default {
  name: "Certificates",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员其他资质证件清单表格数据
      certificatesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 岗位选项
      postOptions: [],
      filteredUsers:[],
      // 证件类型字典
      documentTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        post: null,
        certificate: null,
        awardingBodies: null,
        certificateCode: null,
        certificateDate: null,
        validityPeriodBegin: null,
        validityPeriodEnd: null,
        renewalTime: null,
        documentType: null,
        remarkSpare: null,
        remarkSpare1: null,
        remarkSpare2: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        certificate: [
          { required: true, message: "证书不能为空", trigger: "blur" }
        ],
        certificateCode: [
          { required: true, message: "证书编号不能为空", trigger: "blur" }
        ],
        post: [
          { required: true, message: "岗位不能为空", trigger: "blur" }
        ],
        awardingBodies: [
          { required: true, message: "颁证机构不能为空", trigger: "blur" }
        ],
        certificateDate: [
          { required: true, message: "初领日期不能为空", trigger: "blur" }
        ],
        validityPeriodBegin: [
          { required: true, message: "有效期开始时间不能为空", trigger: "blur" }
        ],
        validityPeriodEnd: [
          { required: true, message: "有效期截止时间不能为空", trigger: "blur" }
        ],
        renewalTime: [
          { required: true, message: "下次复审、换证时间不能为空", trigger: "blur" }
        ],
        documentType: [
          { required: true, message: "证件类型不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.fetchUsers();
    this.getList();
    this.getDicts("document_type").then(response => {
      this.documentTypeOptions = response.data;
    });
    /** 查询字典下拉列表 */
    optionselect().then(response => {
      this.postOptions = response.data;
    });
  },
  methods: {
    /** 查询人员其他资质证件清单列表 */
    getList() {
      this.loading = true;
      listCertificates(this.queryParams).then(response => {
        this.certificatesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    fetchUsers() {
      this.loading=true;
      listUserNoPage().then(response => {
          this.filteredUsers = response.rows;
          this.loading = false;
        }
      );
    },
    // 证件类型字典翻译
    documentTypeFormat(row, column) {
      return this.selectDictLabel(this.documentTypeOptions, row.documentType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        post: null,
        certificate: null,
        awardingBodies: null,
        certificateCode: null,
        certificateDate: null,
        validityPeriodBegin: null,
        validityPeriodEnd: null,
        renewalTime: null,
        documentType: null,
        remark: null,
        remarkSpare: null,
        remarkSpare1: null,
        remarkSpare2: null,
        createBy: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人员其他资质证件清单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCertificates(id).then(response => {
        this.form = response.data;
        this.form.post = parseInt(response.data.post, 10);
        this.open = true;
        this.title = "修改人员其他资质证件清单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCertificates(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCertificates(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除人员其他资质证件清单编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCertificates(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有人员其他资质证件清单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          // return exportCertificates(queryParams);
        this.download('/ledger/certificates/export', {
          ...this.queryParams
        }, `人员其他资质证件清单.xlsx`)
        this.exportLoading = false;
        }).then(response => {
          // this.download(response.msg);
          // this.exportLoading = false;
        }).catch(() => {});
    },
    handleChange(value) {
      console.log("Selected user ID:", value)
    },
  }
};
</script>
